package com.example.poc.food;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.vaadin.exampledata.DataType;
import com.vaadin.exampledata.ExampleDataGenerator;

import lombok.var;

@Configuration
class FoodDataLoader {
  @Bean
  CommandLineRunner initFoods(FoodRepository foodRepository) {
    return args -> {
      if (foodRepository.count() == 0) {
        var generator = new ExampleDataGenerator<>(Food.class, LocalDateTime.now());
        generator.setData(Food::setCode, DataType.FOOD_PRODUCT_EAN);
        generator.setData(Food::setDescription, DataType.FOOD_PRODUCT_NAME);
        List<Food> foods = generator.create(100, new Random().nextInt());
        foodRepository.saveAll(foods);
      }
    };
  }

}
